package menus.common;

import java.awt.geom.Point2D;
import java.util.Vector;

import fr.lri.swingstates.gestures.Gesture;

public abstract class AbstractGestureAnalyzer {
	
	/**
	 * The length of the stroke at each point of the gesture
	 */
	protected double[] lengths;
	
	/**
	 * The total length of the stroke
	 */
	protected double strokeLength;
	
	/**
	 * The Gesture currently analyzed
	 */
	protected Gesture gesture;
	
	/**
	 * The last obtained gesture ID
	 */
	protected int lastResult;
	
	
	
	protected void extractDataFromGesture(Gesture g){
		
		this.gesture = g;
		lastResult = -1;
		
		double distance;
		Point2D cursor;
		
		lengths = new double[gesture.getPoints().size()];
		
		strokeLength = 0;
		
		if (gesture.getPoints().size() > 0) {
			cursor = gesture.getPoints().get(0);
			
			for (int i = 1; i < gesture.getPoints().size(); i++){
				
				distance = gesture.getPoints().get(i).distance(cursor);
				
				strokeLength += distance;
				lengths[i] = strokeLength;
				
				cursor = gesture.getPoints().get(i);
			}
		}
	}
	
	protected int getClosestPointIndex(Gesture gesture, double ratio){
		
		// First we find the first point further than the ratio, 
		// then we check which of this point and the one before is the closest from the ratio
		
		int index;
		for (index = 0; lengths[index] < strokeLength * ratio; index++);
		
		if (lengths[index] - strokeLength * ratio < strokeLength * ratio - lengths[index-1] )
			return index;
		
		return index-1;
		
	}
	
	public int getLastResult(){
		return lastResult;
	}
	
}
